package com.star.bookstore.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.star.bookstore.product.entity.CategoryEntity;
import com.star.bookstore.product.service.CategoryService;
import com.star.bookstore.common.utils.PageUtils;
import com.star.bookstore.common.utils.R;



/**
 * 书本的分类表
 *
 * @author wuLiFei
 * @email wuLiFei.@gmail.com
 * @date 2021-03-07 11:04:13
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查出所有分类，以三级树形结构组装起来
     */
    @RequestMapping("/list/tree")
    public R list(){

        List<CategoryEntity> books = categoryService.listWithTree();
        /*books.forEach(item->{
            System.out.println("书本信息：" + item);
        });*/
        return R.ok().put("data", books);
    }

    @RequestMapping("/list/treeOfTwo")
    public R listOfTwo(){

        List<CategoryEntity> books = categoryService.listWithTree();

        //清空三级分类
        books.forEach(item ->{
            item.getChildren().forEach(item1 ->{
                item1.setChildren(null);
            });
        });

        return R.ok().put("data", books);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{bookId}")
    public R info(@PathVariable("bookId") Long bookId){
		CategoryEntity category = categoryService.getById(bookId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category){

        System.out.println(category.toString());
		categoryService.updateByIdWithAttr(category);

        return R.ok();
    }

    //修改目录结构
    @RequestMapping("/update/sort")
    public R update(@RequestBody CategoryEntity[] category){

        categoryService.updateBatchById(Arrays.asList(category));

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		//categoryService.removeByIds(Arrays.asList(bookIds));

        System.out.println("test..." + Arrays.toString(ids));
        categoryService.removeMenuByIds(Arrays.asList(ids));
        return R.ok();
    }

}
